package com.example.dynamicschedule.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class DateUtils {
    /**
     * 获取批次号
     *
     * @return
     */
    public static String getBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMMdd");
        String format = yyyyMM.format(date);
        String stringDate = format.substring(6, 8);
        if (Integer.valueOf(stringDate) < 15) {
            String batch = format.substring(0, 6)+"01" ;
            return batch;
        }
        String batch = format.substring(0, 6) + "15";
        return batch;
    }
    /**
     * 获取上一个月批次号批次号
     *
     * @return
     */
    public static String getPreBatchNumber() throws ParseException {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMMdd");
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.MONTH,-1);
        Date time = instance.getTime();
        String format = yyyyMM.format(time);
        String stringDate = format.substring(6, 8);
        if (Integer.valueOf(stringDate) < 15) {
            String batch = format.substring(0, 6)+"01" ;
            return batch;
        }
        String batch = format.substring(0, 6) + "15";
        return batch;
    }


    /**
     * 获取年月模糊查询使用
     * @return
     */
    public String getLikeBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        String format = "%"+yyyyMM.format(date)+"%";
        return format;
    }
    /**
     * 获取年月模糊查询使用
     * @return
     */
    public String getPreLikeBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.MONTH,-1);
        Date time = instance.getTime();
        String format = "%"+yyyyMM.format(time)+"%";
        return format;
    }

    /**
     * 获取当前日期
     */
    public String  getNowStr() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMMdd");
        String format = yyyyMM.format(date);
        return format;
    }

    /**
     * 获取当前天
     * @return
     */
    public String getNowDayStr() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("dd");
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.DAY_OF_MONTH,-12);

        String format = yyyyMM.format(date);
        return format;
    }

    /***
     * 获取上一个月的批次号
     * @param date
     * @return
     */
    public static String  getlasetBatchNumber(String date){
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)-1);
        String result = new SimpleDateFormat("yyyyMM").format(instance.getTime());
        return result+date;
    }
    /**
     * 获取一号批次号
     *
     * @return
     */
    public static String getOneMethodBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        String format = yyyyMM.format(date);
        return format+"01";
    }

    /**
     * 根据样式获取time类型
     * @param yyyyMMdd
     * @return
     */
    public static String getNowStr(String yyyyMMdd) {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat(yyyyMMdd);
        String format = yyyyMM.format(date);
        return format;
    }
}
